from flask import Blueprint, jsonify, request
from app.err_no import errno_no
from loguru import logger
import json
import traceback
from app.db import dbOrderManage
from app.globals.publicMethod import is_valid_date
blueprint = Blueprint('order_manage', __name__, url_prefix='/api/order')


@blueprint.route('/v1/create', methods=['POST'])
def create():
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')
        deviceNo = j_data.get('device_no')
        thirdOrderNo = j_data.get('thirdOrderNo')
        skj130OrderTime = j_data.get('skj130OrderTime')
        appLocalOrderId = j_data.get('appLocalOrderId')
        skj130GatewayNo = j_data.get('skj130GatewayNo')
        skj130PosNo = j_data.get('skj130PosNo')
        operator = j_data.get('operator')
        orderAmount = j_data.get('orderAmount')
        chargeAmount = j_data.get('chargeAmount')
        cardQrCodeNum = j_data.get('cardQrCodeNum')
        billState = j_data.get('bill_state')
        payType = j_data.get('pay_type')
        billType = j_data.get('bill_type')
        dishList = j_data.get('dishList')
        billId = j_data.get('billId')

        if diningNo is None or len(diningNo) <= 0 \
            or stallNo is None or len(stallNo) <= 0 \
            or deviceNo is None or len(deviceNo) <= 0 \
            or thirdOrderNo is None or len(thirdOrderNo) <= 0 \
            or skj130OrderTime is None \
            or appLocalOrderId is None or len(appLocalOrderId) <= 0 \
            or skj130GatewayNo is None \
            or skj130PosNo is None or len(skj130PosNo) <= 0 \
            or operator is None \
            or orderAmount is None or isinstance(orderAmount, int) is False \
            or chargeAmount is None or isinstance(chargeAmount, int) is False \
            or cardQrCodeNum is None \
            or billState is None or isinstance(billState, int) is False \
            or payType is None or isinstance(payType, int) is False \
            or billType is None or isinstance(billType, int) is False \
            or dishList is None or len(dishList) <= 0 \
            or billId is None or len(billId) <= 0 \
            or is_valid_date(skj130OrderTime) is False:
            return jsonify(errno_no.ErrServerParam.data(None))
        
        # 判断dishIds数组里是否符合要求
        bPass = True
        for v in dishList:
            dishId = v.get('dishId')
            dishName = v.get('dishName')
            dishPrice = v.get('dishPrice')
            dishCount = v.get('dishCount')
            dishUnit = v.get('dishUnit')

            if dishId is None or len(dishId) <= 0 \
                or dishName is None or len(dishName) <= 0 \
                or dishPrice is None or isinstance(dishPrice, int) is False \
                or dishCount is None or isinstance(dishCount, int) is False \
                or dishUnit is None :
                bPass = False
                break

        if bPass is False:
            return jsonify(errno_no.ErrServerParam.data(None))

        dbOrderManage.createOrder(diningNo, stallNo, billId, deviceNo, skj130OrderTime, appLocalOrderId,
                    skj130GatewayNo, skj130PosNo, orderAmount, 
                    chargeAmount, cardQrCodeNum, billState, 
                    payType, dishList, thirdOrderNo, billType, operator)
        return jsonify(errno_no.OK.data(None))
    except Exception as e:
        logger.error(f'errMsg:{str(e)},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
